<?php

namespace Admin\Model\DAO;

use Zend\Db\TableGateway\AbstractTableGateway;
use Admin\Entity\SerialBook;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\Sql\Sql;
use Zend\Db\Sql\Where;
use Zend\Db\Adapter\Adapter;

class SerialBookTable extends AbstractTableGateway {

    protected $table;
    protected $adapter;
    protected $resultSetPrototype;

    public function __construct(Adapter $adapter) {
        $this->table = 'lib_serialbooks';
        $this->adapter = $adapter;
        $this->resultSetPrototype = new ResultSet();
    }

    public function getBookID($bookBarcode, $serial) {
        $sql = new Sql($this->adapter);
        $select = $sql->select();
        $select->columns(array('bookBarcode', 'serial', 'bookStatus'))
                ->from($this->table);
        $where = new Where();
        $where->equalTo('bookBarcode', $bookBarcode)
                ->equalTo('serial', $serial);
        $select->where($where);
        $statement = $sql->prepareStatementForSqlObject($select);
        $result = $statement->execute();
        $row = $result->current();
        if (!$row)
            return null;
        return $row;
    }

    public function getInfoBook($bookBarcode, $serial) {
        $sql = new Sql($this->adapter);
        $select = $sql->select();
        $select->columns(array('serial', 'bookStatus', 'bookID'))
                ->from(array('sb' => $this->table))
                ->join(array('bs' => 'lib_bookStores'), 'sb.bookBarcode = bs.bookBarcode')
                ->join(array('cl' => 'lib_ciculations'), 'cl.bookID = sb.bookID', array('dateReturn', 'cardBarcode', 'addTime'), 'left')
                ->join(array('bt' => 'lib_bookTypes'), 'bt.bookTypeID = bs.bookTypeID', array('bookTypeName'));
        $where = new Where();
        $where->equalTo('sb.bookBarcode', $bookBarcode)
                ->equalTo('sb.serial', $serial);
        $select->where($where);
        $statement = $sql->prepareStatementForSqlObject($select);
        $result = $statement->execute();
        $row = $result->current();
        if (!$row)
            return null;
        return $row;
    }
    /*
     *  Tim nhuong cuon sach co ma sach la x va chua duoc muon (con trong kho)
     */
    public function getBookExist($bookBarcode) {
        $sql = new Sql($this->adapter);
        $select = $sql->select();
        $select->from(array('sb' => $this->table));
        $where = new Where();
        $where->equalTo('sb.bookStatus', 0)
                ->AND
                ->equalTo('sb.bookBarcode', $bookBarcode);
        $select->where($where);
        $statement = $sql->prepareStatementForSqlObject($select);
        $result = $statement->execute();
        $resultSet = clone $this->resultSetPrototype;
        $resultSet->initialize($result);
        $resultSet->buffer();
        $resultSet->next();
        return $resultSet;
    }
        public function getAllBookFromBarcode($bookBarcode) {
        $sql = new Sql($this->adapter);
        $select = $sql->select();
        $select->from(array('sb' => $this->table));
        $where = new Where();
        $where->equalTo('sb.bookBarcode', $bookBarcode);
        $select->where($where);
        $statement = $sql->prepareStatementForSqlObject($select);
        $result = $statement->execute();
        $resultSet = clone $this->resultSetPrototype;
        $resultSet->initialize($result);
        $resultSet->buffer();
        $resultSet->next();
        return $resultSet;
    }
    public function statisticsDocumentInStore() {
        $sql = new Sql($this->adapter);
        $select = $sql->select();
        $select->columns(array('soluong' => new \Zend\Db\Sql\Expression('COUNT(serial)')))
                ->from(array('sb' => $this->table))
                ->join(array('bs' => 'lib_bookStores'), 'sb.bookBarcode = bs.bookBarcode');
        $where = new Where();
        $where->equalTo('sb.bookStatus', 0);
        $select->where($where);
        $select->group('sb.bookBarcode');
        $select->order(array('sb.bookBarcode'));
        $statement = $sql->prepareStatementForSqlObject($select);
        $result = $statement->execute();
        $resultSet = clone $this->resultSetPrototype;
        $resultSet->initialize($result);
        $resultSet->buffer();
        $resultSet->next();
        return $resultSet;
    }

    public function saveSerialBook(SerialBook $serialBook) {
        $data = $serialBook->getArrayCopy();
        try {
            $this->insert($data);
            return true;
        } catch (Exception $e) {
            echo "loi insert book roi";
        }
        return false;
    }

    public function updateBookStatus($bookID, $data) {
        $this->update(array('bookStatus' => $data), array('bookID' => $bookID));
    }

}

?>
